package jupitermouse.site.juc.demo.aqs.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * <p>
 * </p>
 *
 * @author jupiter
 */
public class ConditionWait implements Runnable {

    private Lock lock;
    private Condition condition;

    public ConditionWait(Lock lock, Condition condition){
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        try {
            lock.lock(); // 竞争锁
            try{
                System.out.println("begin - conditionWait - 阻塞 ");
                condition.await();// 阻塞,让出CPU
                System.out.println("end - conditionWait - 阻塞结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }finally {
            lock.unlock(); // 释放锁
        }
    }
}
